package registnet.util.schedule;

/**
 * An implementation of a task.
 */
public class Task {
		
	/**
	 * The starting time of the task.
	 */
	private double startTime;
	
	/**
	 * The ending time of the task.
	 */
	private double endTime;
	
	/**
	 * The time duration of the task.
	 */
	private double duration;
	
	/**
	 * Default constructor. 
	 */
	public Task(){
		startTime = 0;
		endTime = 0;
		duration = 0;
	}
	
	/**
	 * Checks if the given task is in conflict with the task.
	 * @param task
	 * @return true if the given task is in conflict with the task, and false otherwise.
	 */
	public boolean checkOverlap(Task task){
		if((this.getStartTime()<=task.getStartTime() && task.getStartTime()<this.getEndTime()) || 
		   (this.getStartTime()<task.getEndTime() && task.getEndTime()<=this.getEndTime()))
			return true;
		else
			return false;
	}
	
	/**
	 * Computes for the distance or time interval between the given task and the task.
	 * @param task
	 * @return
	 */
	public double getDistance(Task task){
		double distanceA = 0;
		double distanceB = 0;
		distanceA = task.getStartTime() - this.getEndTime();
		distanceB = this.getStartTime() - task.getEndTime();
		if(distanceA > 0) return distanceA;
		else return distanceB;
	}

	/**
	 * Returns the starting time of the task.
	 * @return the starting time of the task
	 */
	public double getStartTime() {
		return startTime;
	}

	/**
	 * Sets the starting time of the task.
	 * @param startTime
	 */
	public void setStartTime(double startTime) {
		this.startTime = startTime;
	}

	/**
	 * Returns the ending time of the task.
	 * @return the ending time of the task
	 */
	public double getEndTime() {
		return endTime;
	}

	/**
	 * Sets the ending time of the task.
	 * @param endTime
	 */
	public void setEndTime(double endTime) {
		this.endTime = endTime;
	}

	/**
	 * Returns the duration of the task.
	 * @return the duration of the task
	 */
	public double getDuration() {
		return duration;
	}

	/**
	 * Sets the duration of the task.
	 * @param duration
	 */
	public void setDuration(double duration) {
		this.duration = duration;
	}
	
	/**
	 * Process the given <code>String</code> data to create a task.
	 * @param data
	 * @return the resulting task.
	 */
	public static Task processRawData(String data){
		if(data.equals("TBA"))
			return null;
		if(!data.contains("-")){
			System.err.println("****[error] Task processing failed. Invalid Format("+data+").");
			return null;
		}
		
		Task task = new Task();
		String[] time = data.split("-");
		
		/*	
		 * startTime
		 */
		if(time[0].contains(":")){
			String[] tmp = time[0].split(":");
			//hour
			Double startTime = Double.parseDouble(tmp[0]);
			if(startTime>=1 && startTime<7)
				startTime = startTime + 12d;
			//minute
			startTime = startTime + (Double.parseDouble(tmp[0])/60d);
			
			task.setStartTime(startTime);
		}else{
			Double startTime = Double.parseDouble(time[0]);
			if(startTime>=1 && startTime<7)
				startTime = startTime + 12d;
			
			task.setStartTime(startTime);
		}
		
		/*	
		 * endTime
		 */
		if(time[1].contains(":")){
			String[] tmp = time[1].split(":");
			//hour
			Double endTime = Double.parseDouble(tmp[0]);
			if(endTime>=1 && endTime<=7)
				endTime = endTime + 12d;
			//minute
			endTime = endTime + (Double.parseDouble(tmp[0])/60d);
			
			task.setEndTime(endTime);
		}else{
			Double endTime = Double.parseDouble(time[1]);
			if(endTime>=1 && endTime<=7)
				endTime = endTime + 12d;
			task.setEndTime(endTime);
		}
		
		/*
		 * duration
		 */
		task.setDuration(task.getEndTime() - task.getStartTime());
		
		return task;
	}
	
}
